package top.guinguo.hello_00;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.util.Version;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;

/**
 * Created by guin_guo on 2016/6/12.
 */
public class HelloLucene {
    public void index() {
        IndexWriter writer = null;
        try {
            //1.create Directory
            //Directory directory = new RAMDirectory();//create in memery
            Directory directory = FSDirectory.open(new File("E:\\test\\lucene\\index01"));//create in hard memery
            //2.create IndexWriter
            IndexWriterConfig iwc = null;
            iwc = new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35));
            writer = new IndexWriter(directory,iwc);
            //3.create Document Object
            Document doc = null;
            //4.add Field to Document
            File f = new File("E:\\test\\lucene\\example");
            for (File file : f.listFiles()) {
                doc = new Document();
                doc.add(new Field("content",new FileReader(file)));
                doc.add(new Field("filename",file.getName(),Field.Store.YES,Field.Index.NOT_ANALYZED));
                doc.add(new Field("filepath",file.getAbsolutePath(),Field.Store.YES,Field.Index.NOT_ANALYZED));
                //5.add Document to Index Directory by IndexWriter
                writer.addDocument(doc);
            }
        } catch (LockObtainFailedException e) {
            e.printStackTrace();
        } catch (CorruptIndexException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }



    }

    public void searcher() {

        try {
            //1.创建Directory
            Directory directoy = FSDirectory.open(new File("E:\\test\\lucene\\index01"));//create in hard memery
            //2.创建IndexReader
            IndexReader reader = IndexReader.open(directoy);
            //3.根据IndexReader创建IndexSearcher
            IndexSearcher searcher = new IndexSearcher(reader);
            //4.创建搜索的Query,搜索域--content
            QueryParser parser = new QueryParser(Version.LUCENE_35, "content", new StandardAnalyzer(Version.LUCENE_35));
            Query query = parser.parse("ie"); //search what? content included java
            //5.根据searcher搜索并返回TopDocs
            TopDocs topDocs = searcher.search(query, 10);
            //6.根据TopDocs获取ScoreDoc对象
            ScoreDoc[] scoreDocs = topDocs.scoreDocs;
            for (ScoreDoc sc : scoreDocs) {
                //7.根据searcher和ScoreDoc对象获取具体的Document对象
                Document document = searcher.doc(sc.doc);
                //8.根据Document对象获取需要的值
                System.out.println(document.get("filename")+"----"+document.get("filepath"));
            }
            //9.close reader
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }

    }
}
